package com.yx.yinshanjinniu.activitys;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;

import com.umeng.analytics.MobclickAgent;
import com.yx.yinshanjinniu.R;
import com.yx.yinshanjinniu.application.MyApplication;
import com.yx.yinshanjinniu.utils.DataUtils;
import com.yx.yinshanjinniu.utils.TimeCount;
import com.yx.yinshanjinniu.utils.ToolbarUtils;
import com.yx.yinshanjinniu.view.PrivateConfirmDialog;

import java.util.HashMap;
import java.util.Map;

public class SplashActivity extends AppCompatActivity {
    private boolean isFinish;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //防止重复activity实例
        if (!this.isTaskRoot()) {
            Intent intent = getIntent();
            if (intent != null) {
                String action = intent.getAction();
                if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && Intent.ACTION_MAIN.equals(action)) {
                    finish();
                    return;
                }
            }
        }

        //设置状态栏透明
        ToolbarUtils.setStatusBar(this, false, false);
        setContentView(R.layout.activity_splash);
        boolean isAgree = DataUtils.getAgreeState(this);
        if (isAgree) {
            gotoMain();
        } else {
            PrivateConfirmDialog dialog = new PrivateConfirmDialog(this, new PrivateConfirmDialog.PrivateConfirmListener() {
                @Override
                public void onConfirmListener() {
                    MyApplication.getApplication().initOther();

                    //首次安装埋点
                    Map<String, Object> userMap = new HashMap<>();
                    userMap.put("first_start", 1);
                    MobclickAgent.onEventObject(MyApplication.getApplication(), "FIRST_START", userMap);

                    DataUtils.setAgreeState(SplashActivity.this, true);
                    gotoMain();
                }
            });
            if (!dialog.isShowing()) {
                dialog.show();
            }
        }
    }

    private void gotoMain() {
        MyApplication.getApplication().initAllHomeData(null);
        if (DataUtils.isLogin(SplashActivity.this)) {
            MyApplication.getApplication().updateUserDetail();
        }

        new TimeCount(1500, 50) {
            @Override
            public void onFinish() {
                if (!isFinish) {
                    isFinish = true;
                    Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                    startActivity(intent);
                    SplashActivity.this.finish();
                }
            }
        }.start();
    }
}